<?php

/**
 * DepartmentController
 *
 * @author
 * @version
 */
require_once   BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once   BaseUtil::getModulePath("department")  .  '/forms/DepartmentForm.php';
require_once   BaseUtil::getModulePath("department")  .  '/services/DepartmentService.php';
require_once   BaseUtil::getModulePath("dbgrid")  .  '/DBGrid.php';
require_once   BaseUtil::getModulePath("dbgrid")  .  '/DBGridConstant.php';

class Department_DepartmentController extends BaseController {

	public function init(){
        parent::init();
	}


	public function indexAction(){
            $dbgrid = new DBGrid($this->view);
            $dbgrid->setPost($_POST);
            $dbgrid->setModule("/department/department");
            if(AAService::isAllowed("department", "edit")){
                $dbgrid->addModuleAction("edit",$this->_translate->translate("editBtn"),"edit");
            }
            if(AAService::isAllowed("department", "deletesome")){
                $dbgrid->addModuleAction("delete",$this->_translate->translate("delBtn"),"deletesome");
            }
            if(AAService::isAllowed("department", "add")){
                $dbgrid->addModuleAction("add",$this->_translate->translate("addBtn"),"add");
            }
            
    //        $itemActions = array(
    //            "Edit" => "edit",
    //            "Delete" => "del"
    //        );
    //        $dbgrid->setItemActions($itemActions);

            $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "name",$this->_translate->translate("deName"));
            $dbgrid->addValueFilter("is_delete",0);
            $dbgrid->addFetchTable("departments","*");
            $dbgrid->addFetchColumn("name",$this->_translate->translate("deName"),"edit");
            $dbgrid->setPageTitle($this->_translate->translate("searchTitle"));
            $this->view->dbGrid = $dbgrid->render();
	}
	public function addAction() {
		$form = new Department_DepartmentForm();
		$this->view->form = $form;
	}
	public function subaddAction() {
		$name=HttpUtil :: GetPostString('name');
		$form = new Department_DepartmentForm();
		if (!$form->isValid($_POST)) {
			$this->view->form = $form;
			return $this->render('add');
		}
		$department = array ('name'=> $name,
                             'created'=> date("Y-m-d H:i:s"),
                             'modified'=> date("Y-m-d H:i:s")
                             );
		$departmentService = new DepartmentService();
		$departmentService->saveDepartment($department);
		//$rows_affected = $this->db->insert('department', $row);
		$this->_redirector->gotoUrl('department/department');
	}
	public function editAction(){

		$id = $this->_getParam('id');
		$departmentService = new DepartmentService();
		$department = $departmentService->getDepartmentByID($id);
		$option =array();
		$option['name']=$department['name'];
		$option['id']=$id;
		$form = new Department_DepartmentForm($option);
		$this->view->form = $form;

	}
	public function subeditAction() {
		$name=HttpUtil :: GetPostString('name');
		$id=HttpUtil :: GetPostString('department_id');
        $form = new Department_DepartmentForm(array("id" =>$id,"name"=>$name));
		if (!$form->isValid($_POST)) {
			$this->view->form = $form;
            return $this->render('edit');
		}
		$department = array ('name'=> $name , 'modified'=> date("Y-m-d H:i:s"));
		$departmentService = new DepartmentService();
		$departmentService->updateDepartment($id,$department);
		$this->_redirector->gotoUrl('department/department');
	}
	public function deleteAction(){
		//$request = $this->_getParam();
		$id = $this->_getParam('id');
		$departmentService = new DepartmentService();
		$department = $departmentService->deleteDepartment($id);
		$this->_redirector->gotoUrl('department/department');
		//		print_r($department->name);exit;
	}

    public function deletesomeAction(){
        $idsStr = HttpUtil::getPostValue("selectIds");
        $ids = explode(",",$idsStr);
		$departmentService = new DepartmentService();
		$departmentService->deleteAllDepartments($ids);
		$this->_redirector->gotoUrl('department/department');
    }
}